package org.prail.threads;

import javax.swing.table.DefaultTableModel;

import org.prail.net.core.Download;
import org.prail.ui.DownloadTable;
import org.prail.ui.Mp3Frame;
import org.prail.ui.utils.CustomProgressBar;
import org.prail.wrappers.SearchResult;

public class DownloadThread extends Thread {
	private CustomProgressBar bar = null;
	private SearchResult result = null;

	public DownloadThread(CustomProgressBar bar, SearchResult result, int row) {
		this.bar = bar;
		bar.setStringPainted(true);
		bar.setMinimum(0);
		bar.setMaximum(100);
		this.result = result;
	}

	@Override
	public void run() {
		Mp3Frame.jTabbedPane1.setSelectedIndex(1);
		new Download(this, result);
		bar.setString("Downloaded");
		update();
	}
	

	private void update() {
		DefaultTableModel m = (DefaultTableModel) DownloadTable.get()
				.getModel();
		m.fireTableCellUpdated(DownloadTable.getRow(result), 3);
	}

	public void setValue(final int percent) {
		bar.setValue(percent);
		bar.setString(percent + "%");
		update();
	}

}
